<?php

namespace App\Console\Commands;

use App\Model\dlt;
use Illuminate\Console\Command;
use App\Model\ssq;
use Illuminate\Support\Facades\Mail;

class ssqHandle extends Command
{
    public $url = "https://cn.apihz.cn/api/caipiao/shuangseqiu.php?id=10001488&key=f949fd7039cc34123cbaf1481afc29fb";
    public $dlt_url = "https://cn.apihz.cn/api/caipiao/daletou.php?id=10001488&key=f949fd7039cc34123cbaf1481afc29fb";
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ssqHandle';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */

    public function handle(){
        $obj_qihao = (new ssq)->getLastQh();
        $obj_dlt_qihao = (new dlt)->getLastQh();
        $res = curlGet($this->url);
        $dlt_res = curlGet($this->dlt_url);
        $res = json_decode($res,true);
        $dlt_res = json_decode($dlt_res,true);
        unset($res['name']);
        unset($dlt_res['name']);
        if($res['code'] == 200){
            unset($res['code']);
            if(intval($res['qihao']) != $obj_qihao->qihao){
                ssq::create($res);
            }
            if($obj_qihao->no1num == '_' || $obj_qihao->no1num == null && $res['no1num'] != '_'){
                ssq::where(['qihao'=>$obj_qihao->qihao])->update($res);
            }
        }else{
            //发邮件通知
            Mail::raw('获取数据失败', function($message){
                $message->to('171161005@qq.com');
                $message->subject('获取数据失败');
            });
        }
        if($dlt_res['code'] == 200){
            unset($dlt_res['code']);
            if(intval($dlt_res['qihao']) != $obj_dlt_qihao->qihao){
                dlt::create($dlt_res);
            }
            if($obj_qihao->no1num == '_' || $obj_qihao->no1num == null && $res['no1num'] != '_'){
                dlt::where(['qihao'=>$obj_dlt_qihao->qihao])->update($dlt_res);
            }
        }else{
            //发邮件通知
            Mail::raw('获取数据失败', function($message){
                $message->to('171161005@qq.com');
                $message->subject('大获取数据失败');
            });
        }
    }

    //获取历史记录
//    public function handle()
//    {
//        $year = 2013;
//        $url = "https://vip.apihz.cn/api/caipiao/shuangseqiu.php?id=10001488&key=f949fd7039cc34123cbaf1481afc29fb&qh=";
//
//        while($year < 2025){
//            $num = 1;
//
//            if($num < 10){
//                $str = $year.'00'.$num;
//            }elseif ($num < 100) {
//                $str = $year.'0'.$num;
//            }else{
//                $str = $year.$num;
//            }
//            $url .= $str;
//            $res = curlGet($url);
//            $url = "https://vip.apihz.cn/api/caipiao/shuangseqiu.php?id=10001488&key=f949fd7039cc34123cbaf1481afc29fb&qh=";
//            $res = json_decode($res,true);
//
//            while ($res['code'] != 400){
//                $r = ssq::create($res);
//                $num++;
//
//                if($num < 10){
//                    $str = $year.'00'.$num;
//                }elseif ($num < 100) {
//                    $str = $year.'0'.$num;
//                }else{
//                    $str = $year.$num;
//                }
//                $url .= $str;
//                $res = curlGet($url);
//                $url = "https://vip.apihz.cn/api/caipiao/shuangseqiu.php?id=10001488&key=f949fd7039cc34123cbaf1481afc29fb&qh=";
//                $res = json_decode($res,true);
//
//                if($r){
//                    echo $str.'---';
//                }else{
//                    echo 'fail';
//                }
//            }
//            if($res['code'] != 200) {
//                $year++;
//                sleep(60);
//            }
//        }
//    }

}
